//
//  BJScrollerViewController.m
//  SZBank
//
//  Created by rimi on 16/7/21.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJScrollerViewController.h"

@interface BJScrollerViewController ()<UIScrollViewDelegate>
@property (nonatomic)UIButton *cancelButton;/**< 返回 */
@property (nonatomic)UIView *navigationView;/**< 导航视图 */
@property (nonatomic)UIScrollView *scrollView;/**<滚动视图 */
@property (nonatomic, strong) UIPageControl *pageControl;//页面控件
@property (nonatomic)UILabel *titleLabel;/**< 标题label */
@end

@implementation BJScrollerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self.navigationView addSubview:self.cancelButton];
    [self.view addSubview:self.navigationView];
    if (self.dataArray.count != 0) {
        for (int i = 0;i < self.dataArray.count; i ++) {
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(KWIDTH * i, 0, KWIDTH, self.scrollView.frame.size.height)];
            imageView.image = [LocalDataManager getImageWithURL:self.dataArray[i]];
            [self.scrollView addSubview:imageView];
        }
        self.scrollView.contentSize = CGSizeMake(KWIDTH * self.dataArray.count, self.scrollView.frame.size.height);
        
    }
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    self.titleLabel.text = [NSString stringWithFormat:@"1/%lu",(unsigned long)self.dataArray.count];
    [self.navigationView addSubview:self.titleLabel];
    
}
#pragma mark -- UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.titleLabel.text = [NSString stringWithFormat:@"%ld/%lu",((NSInteger)floor((scrollView.contentOffset.x - KWIDTH / 2) / KWIDTH) + 2),(unsigned long)self.dataArray.count];
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
 
//设置当前页
    self.pageControl.currentPage = floor((scrollView.contentOffset.x - KWIDTH / 2) / KWIDTH) + 1;

}
#pragma mark -- actions
- (void)action_buttonClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark -- getters
- (UIButton *)cancelButton{
    if (!_cancelButton) {
        _cancelButton = [[UIButton alloc]initWithFrame:CGRectMake(0, KHEIGHT * 0.02, KWIDTH * 0.1, KHEIGHT * 0.04)];
        [_cancelButton setTitle:@"返回" forState:UIControlStateNormal];
        [_cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        _cancelButton.backgroundColor = [UIColor clearColor];
        [_cancelButton addTarget:self action:@selector(action_buttonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _cancelButton;
}

- (UIView *)navigationView{
    if (!_navigationView) {
        _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KWIDTH, KHEIGHT * 0.06)];
        _navigationView.backgroundColor = [UIColor colorWithWhite:0.798 alpha:0.562];
    }
    return _navigationView;
}
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, KHEIGHT * 0.06, KWIDTH , KHEIGHT * 0.9)];
        _scrollView.center = self.view.center;
        _scrollView.backgroundColor = [UIColor clearColor];
        
        //设置内容大小
        _scrollView.contentSize = CGSizeMake(KWIDTH * 11, KHEIGHT * 0.9);
        _scrollView.contentOffset = CGPointMake(0, 0);
        
        //隐藏内容进度条
        _scrollView.showsHorizontalScrollIndicator = NO;
        _scrollView.showsVerticalScrollIndicator  = NO;
        
        //分页
        _scrollView.pagingEnabled = YES;
        
        //回弹效果
        _scrollView.bounces = NO;
        
        //设置代理
        _scrollView.delegate = self;
        
    }
    return _scrollView;
}
- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 0, 340, 30)];
        _pageControl.center = CGPointMake(self.view.center.x, CGRectGetMaxY(self.scrollView.frame) - KHEIGHT * 0.1);
        _pageControl.numberOfPages = self.dataArray.count;
        _pageControl.currentPage = 0;
        //主色调
        _pageControl.pageIndicatorTintColor = [UIColor whiteColor];
        //当前页颜色
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    }
    return _pageControl;
}

- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KWIDTH * 0.6, KHEIGHT * 0.04)];
        _titleLabel.center = CGPointMake(KWIDTH * 0.5, KHEIGHT *  0.04);
        _titleLabel.font = [UIFont systemFontOfSize:FONT_SIZE];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _titleLabel;
}
@end
